/*
 * Copyright 2011 es-planning Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.arctos.ursus.extension.net;

import java.io.IOException;
import java.net.SocketAddress;
import java.net.SocketException;

/**
 * Handler for socket sessions.
 * 
 */
public class SocketSessionHandler {

    /** Socket session. */
    SocketSession session;

    /**
     * Constructor.
     * 
     * @param session Session
     * @throws IOException
     */
    SocketSessionHandler(SocketSession session) throws IOException {
        this.session = session;
    }

    /**
     * Get bound local host address.
     * 
     * @return Local host address
     */
    public SocketAddress getLocalAddress() {
        return session.getLocalAddress();
    }

    /**
     * Get address of connected remote host.
     * 
     * @return Remote host address
     */
    public SocketAddress getRemoteAddress() {
    	return session.getRemoteAddress();
    }
    
    /**
     * Close session.
     * 
     */
    public void close() {
        session.closeSession();
    }

    /**
     * Accept send request.
     * 
     * @param request Send request data
     * @return "true" if accepted, "false" if unable to be accepted
     */
    public boolean send(SendRequest request) {
        return session.send(request);
    }

    /**
     * Get value of SO_RCVBUF socket option.
     * 
     * @return SO_RCVBUF Socket option
     * @throws SocketException 
     * @see java.net.Socket#getReceiveBufferSize()
     */
    public int getReceiveBufferSize() throws SocketException {
        return session.getReceiveBufferSize();
    }

    /**
     * Get value of SO_REUSEADDR socket option.
     * 
     * @return SO_REUSEADDR Socket option
     * @throws SocketException 
     * @see java.net.Socket#getReuseAddress()
     */
    public boolean getReuseAddress() throws SocketException {
        return session.getReuseAddress();
    }

    /**
     * Get value of SO_TIMEOUT socket option.
     * 
     * @return SO_TIMEOUT Socket option
     * @throws SocketException 
     * @see java.net.Socket#getSoTimeout()
     */
    public int getSoTimeout() throws SocketException {
        return session.getSoTimeout();
    }

    /**
     * Configure SO_TIMEOUT socket option.
     * 
     * @param timeout SO_TIMEOUT Socket option
     * @throws SocketException 
     * @see java.net.Socket#setSoTimeout(int)
     */
    public void setSoTimeout(int timeout) throws SocketException {
        session.setSoTimeout(timeout);
    }

    /**
     * Get value of SO_KEEPALIVE socket option.
     * 
     * @return SO_KEEPALIVE Socket option
     * @throws SocketException 
     * @see java.net.Socket#getKeepAlive()
     */
    public boolean getKeepAlive() throws SocketException {
        return session.getKeepAlive();
    }

    /**
     * Configure SO_KEEPALIVE socket option.
     * 
     * @param keepAlive SO_KEEPALIVE Socket option
     * @throws SocketException 
     * @see java.net.Socket#setKeepAlive(boolean)
     */
    public void setKeepAlive(boolean keepAlive) throws SocketException {
        session.setKeepAlive(keepAlive);
    }

    /**
     * Get value of SO_SNDBUF socket option.
     * 
     * @return SO_SNDBUF Socket option
     * @throws SocketException 
     * @see java.net.Socket#getSendBufferSize()
     */
    public int getSendBufferSize() throws SocketException {
        return session.getSendBufferSize();
    }

    /**
     * Configure SO_SNDBUF socket option.
     * 
     * @param size SO_SNDBUF Socket option
     * @throws SocketException 
     * @see java.net.Socket#setSendBufferSize(int)
     */
    public void setSendBufferSize(int size) throws SocketException {
        session.setSendBufferSize(size);
    }

    /**
     * Get value of traffic class.
     * 
     * @return Traffic class
     * @throws SocketException 
     * @see java.net.Socket#getTrafficClass()
     */
    public int getTrafficClass() throws SocketException {
        return session.getTrafficClass();
    }

    /**
     * Configure traffic class.
     * 
     * @param trafficClass Traffic class
     * @throws SocketException
     * @see java.net.Socket#setTrafficClass(int)
     */
    public void setTrafficClass(int trafficClass) throws SocketException {
        session.setTrafficClass(trafficClass);
    }

    /**
     * Get value of SO_LINGER socket option.
     * 
     * @return SO_LINGER Socket option
     * @throws SocketException
     * @see java.net.Socket#getSoLinger()
     */
    public int getSoLinger() throws SocketException {
        return session.getSoLinger();
    }

    /**
     * Configure SO_LINGER socket option.
     * 
     * @param on Valid / Invalid
     * @param linger SO_LINGER Socket option
     * @throws SocketException 
     * @see java.net.Socket#setSoLinger(boolean, int)
     */
    public void setSoLinger(boolean on, int linger) throws SocketException {
        session.setSoLinger(on, linger);
    }

    /**
     * Get value of TCP_NODELAY socket option.
     * 
     * @return TCP_NODELAY Socket option
     * @throws SocketException 
     * @see java.net.Socket#getTcpNoDelay()
     */
    public boolean getTcpNoDelay() throws SocketException {
        return session.getTcpNoDelay();
    }

    /**
     * Configure TCP_NODELAY socket option.
     * 
     * @param tcpNoDelay TCP_NODELAY Socket option
     * @throws SocketException
     * @see java.net.Socket#setTcpNoDelay(boolean)
     */
    public void setTcpNoDelay(boolean tcpNoDelay) throws SocketException {
        session.setTcpNoDelay(tcpNoDelay);
    }
}
